#博客详情页测试
import datetime
import time

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from Common.Untils import BlogDriver


class BlogDetail:
    url=""
    driver=""
    def __init__(self):
        self.driver=BlogDriver.driver
        self.url="http://8.137.19.140:9090/blog_detail.html?blogId=7681"

    #登陆状态下
    def DetailByLogin(self):
        nums = self.driver.find_elements(By.CSS_SELECTOR, "body > div.container > div.right > div")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(" + str(len(nums)) + ") > a").click()
        self.url = self.driver.current_url
        self.driver.back()
        #通过链接访问
        self.driver.get(self.url)
        #点击“编辑”更改标题后点击“更新文章”，检查标题是否更改
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)").click()
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#title").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("更改标题测试")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.get(self.url)
        time.sleep(1)
        ret=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.title").text
        print(ret)
        time.sleep(1)
        assert ret=="更改标题测试"
        #检查更新后时间是否准确
        ret=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date").text
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
        assert ret==now
        #检查博客内容部分
        self.driver.find_element(By.CSS_SELECTOR,"#h2-u5728u8FD9u91CCu5199u4E0Bu4E00u7BC7u535Au5BA2")
        #测试删除文章功能
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        time.sleep(1)
        #取消删除，刷新页面
        self.driver.switch_to.alert.dismiss()
        self.driver.back()
        try:
           self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(" + str(len(nums)) + ") > div.title")
        except NoSuchElementException:
            print("取消删除但文章仍被删除")
        self.driver.get(self.url)
        #点击删除
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        time.sleep(1)
        self.driver.switch_to.alert.accept()
        #查看列表页是否已经删除
        try:
           self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child("+str(len(nums))+") > div.title")
           print("文章删除失败，仍存在列表页")
        except NoSuchElementException:
           print("文章删除成功")

        #删除后通过链接是否能访问
        self.driver.get(self.url)
        try:
           self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)")
           print("删除后仍可通过链接访问")
        except NoSuchElementException:
           print("删除后无法通过链接访问")
        #清理测试数据
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(4)").click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(" + str(len(nums)-1) + ") > a").click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        time.sleep(1)
        self.driver.switch_to.alert.accept()


    #未登陆下详情页
    def DetailByNotlogin(self):
        #注销登陆
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()
        self.driver.get(self.url)
        #无法访问，仍在登陆页
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container-login > div > h3")

# Detail=BlogDetail()
# Detail.DetailByLogin()
# Detail.DetailByNotlogin()
